import request from '@/utils/request'

// 查询盒子信息列表
export function listBoxinfo(query) {
  return request({
    url: '/deviceBox/boxinfo/list',
    method: 'get',
    params: query
  })
}

// 查询盒子信息详细
export function getBoxinfo(id) {
  return request({
    url: '/deviceBox/boxinfo/' + id,
    method: 'get'
  })
}

// 新增盒子信息
export function addBoxinfo(data) {
  return request({
    url: '/deviceBox/boxinfo',
    method: 'post',
    data: data
  })
}

// 修改盒子信息
export function updateBoxinfo(data) {
  return request({
    url: '/deviceBox/boxinfo',
    method: 'put',
    data: data
  })
}

// 删除盒子信息
export function delBoxinfo(id) {
  return request({
    url: '/deviceBox/boxinfo/' + id,
    method: 'delete'
  })
}

// 更新默认插件
export function updateDefaultPlugin(data) {
  return request({
    url: '/plugin/plugin/updateDefault',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: data
  })
}

// 执行设备命令
export function executeDeviceCmd(data) {
  return request({
    url: '/deviceBox/boxinfo/cmd',
    method: 'get',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    params: data
  })
}

// 获取设备应用列表
export function getDeviceAppList(mac, id) {
  return request({
    url: '/appInfo/info/device/app/list',
    method: 'get',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    params: { mac, id }
  })
}

// 获取可选应用列表（选择下发安装应用）
export function getSelectableAppList(isCommon = 0,type = 1,columnId = '') {
  return request({
    url: '/appInfo/info/list',
    method: 'get',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    params: { isCommon,type,columnId }
  });
}

// 下发选择的应用
export function issueSelectedApp(appId, mac) {
  return request({
    url: '/appInfo/info/select/issue',
    method: 'post',
    headers: { 'Content-Type': 'application/json' },
    data: { appId, mac }
  });
}

// 卸载应用
export function uninstallDeviceApp(appId, mac) {
  return request({
    url: '/appInfo/info/delete/issue',
    method: 'delete',
    headers: { 'Content-Type': 'application/json' },
    data: { appId, mac }
  });
}

// 新的下发指令API
export function sendDeviceCommand(cmd, data, mac) {
  const requestData = {
    cmd,
    data,
    mac
  };
  
  // 调试信息
  console.log('API发送数据:', requestData);
  
  return request({
    url: '/deviceBox/boxinfo/cmd',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    data: requestData
  })
}

// 盒子压测接口
export function pressTest(sn) {
  return request({
    url: '/producer/press/create/press',
    method: 'put',
    headers: { 'Content-Type': 'application/json' },
    data: { sn }
  });
}
